﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Threading;

namespace mediaPrime.Controls
{
    public class MPListBox : ListBox
    {
        public static readonly RoutedCommand MPItemClickCommand = new RoutedCommand();

        public event mediaPrime.Controls.MPMenu.MPMenuItemClickedEventHandler ItemClicked;

        static MPListBox()
        {
            // Use another default style than the base class
            FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
                typeof(MPListBox), new FrameworkPropertyMetadata(typeof(MPListBox))
            );
        }
        public MPListBox()
        {
            var mpItemClickCommandBinding = new CommandBinding(MPItemClickCommand);
            mpItemClickCommandBinding.CanExecute += mpItemClickCommandBinding_CanExecute;
            mpItemClickCommandBinding.Executed += mpItemClickCommandBinding_Executed;

            CommandBindings.Add(mpItemClickCommandBinding);
            this.Loaded += MPListBox_Loaded;
          
        }

        private void MPListBox_Loaded(object sender, RoutedEventArgs e)
        {
            var traversalRequest = new TraversalRequest(FocusNavigationDirection.Next);
            MoveFocus(traversalRequest);
        }

        private void mpItemClickCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (e.OriginalSource is MPTileButton)
            {
                var dataContextButton = (e.OriginalSource as MPTileButton).DataContext;
                var listBoxItem = ItemContainerGenerator.ContainerFromItem(dataContextButton) as ListBoxItem;
                listBoxItem.IsSelected = true;
            }

            var menuItemClicked = ItemClicked;
            if (menuItemClicked != null)
                menuItemClicked(sender, new MPMenuItemClickedEventArgs() { Item = e.Parameter });
        }

        private void mpItemClickCommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (e.OriginalSource is MPTileButton)
                e.CanExecute = (e.OriginalSource as MPTileButton).IsEnabled;
            else
                e.CanExecute = true;
        }

        protected override void OnItemsSourceChanged(System.Collections.IEnumerable oldValue, System.Collections.IEnumerable newValue)
        {
            base.OnItemsSourceChanged(oldValue, newValue);
            this.Dispatcher.BeginInvoke((Action)delegate
            {
                var traversalRequest = new TraversalRequest(FocusNavigationDirection.Previous);
                MoveFocus(traversalRequest);
            }, DispatcherPriority.Render);
        }


      
        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Right)
            {
                e.Handled = true;
                var traversalRequest = new TraversalRequest(FocusNavigationDirection.Right);
                MoveFocus(traversalRequest);
            }
            else if (e.Key == Key.Left)
            {
                e.Handled = true;
                var traversalRequest = new TraversalRequest(FocusNavigationDirection.Left);
                MoveFocus(traversalRequest);
            }

            base.OnPreviewKeyDown(e);
        }

        public GroupStyle DefaultGroupStyle
        {
            get { return (GroupStyle)GetValue(DefaultGroupStyleProperty); }
            set { SetValue(DefaultGroupStyleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DefaultGroupStyle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DefaultGroupStyleProperty =
            DependencyProperty.Register("DefaultGroupStyle", typeof(GroupStyle), typeof(MPListBox), new UIPropertyMetadata(null, DefaultGroupStyleChanged));

        private static void DefaultGroupStyleChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            ((MPListBox)o).SetDefaultGroupStyle(e.NewValue as GroupStyle);
        }

        private void SetDefaultGroupStyle(GroupStyle defaultStyle)
        {
            if (defaultStyle == null)
            {
                return;
            }

            if (this.GroupStyle.Count == 0)
            {
                this.GroupStyle.Add(defaultStyle);
            }
        }

    }
}
